﻿using System;
using System.Collections.Generic;
using System.Linq;
using AzureDiagMon.Common.Helpers;

namespace AzureDiagMon.Common.Mvvm
{
    /// <summary>
    /// Generic view model for entity editors
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public abstract class EntityEditorViewModel<TEntity> : ChildViewModel
        where TEntity : class, new()
    {
        public EntityViewModel<TEntity> EntityViewModel { get; private set; }

        private TEntity entity;
        public TEntity Entity
        {
            get { return entity; }
            private set
            {
                entity = value;
                EntityViewModel = CreateEntityViewModel(entity);
            }
        }

        protected EntityEditorViewModel()
        {
            Entity = new TEntity();
        }

        protected EntityEditorViewModel(TEntity entity)
        {
            Entity = entity;
        }

        public abstract EntityViewModel<TEntity> CreateEntityViewModel(TEntity entity);

        protected bool Validate(bool showMessageBox = true)
        {
            //ICollection<ValidationResult> errors;
            //var result = DataValidator.TryValidate(Entity, out errors);
            //if (!result)
            //{
            //    ShowValidationResultsMessageBox(errors);
            //}
            //return result;
            return true;
        }

        //public void ShowValidationResultsMessageBox(ICollection<System.ComponentModel.DataAnnotations.ValidationResult> validationResults)
        //{
        //    InteractionService.ShowMessage(String.Format("Kérem javítsa a következő hibákat: \n - {0}",
        //                                     String.Join("\n - ", validationResults.Select(f => f.ToString()))),
        //                                   "Hibás adatok", MessageCategory.Error);
        //}

        //public ConfirmationResult ShowConfirmation(string msg, string title = "Törlés")
        //{
        //    return InteractionService.ShowConfirmation(msg, title, MessageCategory.Question,
        //                                               ConfirmationCategory.OkCancel);
        //}
    }
}
